package org.dromara.third.convert.personViolationAlarm.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.PersonViolationAlarm;
import org.dromara.system.mapper.PersonViolationAlarmMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.personViolationAlarm.dto.PersonViolationAlarmDTO;
import org.dromara.third.convert.personViolationAlarm.validator.PersonViolationAlarmValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertPersonViolationAlarmController {

    @Autowired
    private PersonViolationAlarmMapper personViolationAlarmMapper;

    @PostMapping("/rywgbj")
    @Transactional
    public R<Void> addPersonViolationAlarm(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        PersonViolationAlarmDTO personViolationAlarmDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            personViolationAlarmDTO = JSONObject.parseObject(decrypt, PersonViolationAlarmDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, personViolationAlarmDTO, new PersonViolationAlarmValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        PersonViolationAlarm personViolationAlarm = assemblePersonViolationAlarmData(orgCode, personViolationAlarmDTO);
        int count = personViolationAlarmMapper.countWithOutDelFlag(personViolationAlarm.getId());
        if (count == 0) {
            personViolationAlarmMapper.insert(personViolationAlarm);
        } else {
            personViolationAlarmMapper.updateWithOutDelFlag(personViolationAlarm);
        }
        return R.ok();
    }

    private PersonViolationAlarm assemblePersonViolationAlarmData(String orgCode, PersonViolationAlarmDTO dto) {
        PersonViolationAlarm personViolationAlarm = new PersonViolationAlarm();
        try {
            personViolationAlarm.setId(dto.getDataId());
            personViolationAlarm.setZyid(dto.getZyid());
            personViolationAlarm.setBjsj(DateUtils.parseDate(dto.getBjsj()));
            personViolationAlarm.setBjlx(dto.getBjlx());
            personViolationAlarm.setBjryxm(dto.getBjryxm());
            personViolationAlarm.setBjrylx(dto.getBjrylx());
            personViolationAlarm.setLongitude(dto.getLongitude());
            personViolationAlarm.setLatitude(dto.getLatitude());
            personViolationAlarm.setXjsj(DateUtils.parseDate(dto.getXjsj()));
            personViolationAlarm.setDelFlag(Long.parseLong(dto.getDelFlag()));
            personViolationAlarm.setCreateDept(Long.parseLong(dto.getCreateDept()));
            personViolationAlarm.setCreateBy(Long.parseLong(dto.getCreateBy()));
            personViolationAlarm.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            personViolationAlarm.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            personViolationAlarm.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            personViolationAlarm.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装人员违规报警信息异常");
        }
        return personViolationAlarm;
    }
}
